<?php

 function GetUserIp()
 {
    //check ip from share internet
    if (!empty($_SERVER['HTTP_CLIENT_IP'])){
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    //to check ip is pass from proxy
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
 }
 
 function SetUser()
 {
    $mysqli = new mysqli(---);
        if ($mysqli->connect_error){die ("Connect error: " . $mysqli->connect_error );}
        
    $user_ip = GetUserIp();
    
    $date = new DateTime();
    $date2 = $date->format('Y-m-d H:i:s');
    
    $query = ("
                INSERT INTO user_visits 
                    (user_ip, user_timestep)
                VALUES
                    ('$user_ip', '$date2')
              ");
    
     $mysqli->query($query);
        if ($mysqli->error){die ("Query error: " . $mysqli->error );}
        
     $mysqli->close();   
 }
 
 // Check how often the sql 
 function GetVisits()
 {
    $date = new DateTime();
    $date->modify('-15 second');
    $visit_counter = $date->format('Y-m-d H:i:s');  
       
     
     $mysqli = new mysqli(---);
        if ($mysqli->connect_error){die ("Connect error: " . $mysqli->connect_error );}
      
      $user_ip = GetUserIp();  
      $query = ("
                SELECT 
                    * 
                FROM 
                    user_visits
                WHERE 
                    user_ip = '$user_ip'
                AND        
                    user_timestep > '$visit_counter'
        ");
      
      $result = $mysqli->query($query);
        if ($mysqli->error){die ("Query error: " . $mysqli->error );}
        
        return $result->num_rows;  
        
$mysqli->close();
}//end func
 
function GetBanlist()
 {
     $date = new DateTime();

    $visit_counter = $date->format('Y-m-d H:i:s');  
       
     
     $mysqli = new mysqli(---);
        if ($mysqli->connect_error){die ("Connect error: " . $mysqli->connect_error );}
      
      $user_ip = GetUserIp();  
      $query = ("
                SELECT 
                    * 
                FROM 
                    banlist
                WHERE 
                    banned_ip = '$user_ip'
                AND        
                    banned_timestep > '$visit_counter'
        ");
      
      $result = $mysqli->query($query);
        if ($mysqli->error){die ("Query error: " . $mysqli->error );}
        
        return $result->num_rows;  
        
$mysqli->close();
 }
 
function SetBanIp()
 {
    $date = new DateTime();
    $date->modify('+1 hour');
    $date2 = $date->format('Y-m-d H:i:s');   
    
    
    $mysqli = new mysqli(---);
        if ($mysqli->connect_error){die ("Connect error: " . $mysqli->connect_error );}
        
    $user_ip = GetUserIp();
    
    $query = ("
                INSERT INTO banlist
                    (banned_ip, banned_timestep)
                VALUES
                    ('$user_ip', '$date2')
              ");
    
     $mysqli->query($query);
        if ($mysqli->error){die ("Query error: " . $mysqli->error );}
        
     $mysqli->close();
 }



 
if(GetBanlist() > 0) {
    echo 'you are banned - visit us later';
    exit;
}

SetUser();
if(GetVisits() == 2) {
    echo 'Warning you requested the Website a 2. time in 15 seconds <br>
after 5 requests in 15 Seconds get your IP Banned!'; }
elseif(GetVisits() == 3)
    echo 'Warning you requested the Website a 3. time in 15 seconds <br>';
elseif(GetVisits() >= 5) {
    echo 'Your IP is Banned for 1 hour';
    SetBanIP();
}
?>